package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.organization.OrganizationAddRequest;
import cn.edu.ujs.coor.request.organization.OrganizationPageRequest;
import cn.edu.ujs.coor.request.organization.OrganizationUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.response.OrganizationResponse;
import cn.edu.ujs.coor.response.PageResponse;
import cn.edu.ujs.coor.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 组织Controller
 *
 * @author 柳曾雄
 * @date 2018-08-31
 */
@RestController
@RequestMapping(value = "/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 分页查询组织信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public JsonResponse list (@RequestBody OrganizationPageRequest request, User user) {
        PageResponse<OrganizationResponse> response = organizationService.page(request);
        return JsonResponse.success(response);
    }

    /**
     * 更新组织信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResponse add (@Valid @RequestBody OrganizationAddRequest request, User user) {
        request.setUserId(user.getId());
        organizationService.add(request);
        return JsonResponse.success();
    }
    
    /**
     * 更新组织信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResponse update (@Valid @RequestBody OrganizationUpdateRequest request) {
        organizationService.update(request);
        return JsonResponse.success();
    }

    /**
     * 删除组织信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody IdRequest request) {
        organizationService.delete(request);
        return JsonResponse.success();
    }
}
